import { createRouter, createWebHistory } from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import Dashboard from '../views/Dashboard.vue'

const routes = [
  {
    path: '/',
    name: 'login',
    component: Login
  },
  {
    path: '/home',
    name: 'home',
    component: Home,
    children: [
      {
        path: '',
        name: 'dashboard',
        component: Dashboard
      },
      {
        path: 'prod/job-center',
        name: 'prod-job-center',
        component: () => import('../components/jobs/JobCenter.vue'),
        props: { env_id: 'PROD' }
      },
      {
        path: 'prod/schedule-center',
        name: 'prod-schedule-center',
        component: () => import('../components/schedule/ScheduleCenter.vue'),
        props: { env_id: 'PROD' }
      },
      {
        path: 'local/job-center',
        name: 'local-job-center',
        component: () => import('../components/jobs/JobCenter.vue'),
        props: { env_id: 'LOCAL' }
      },
      {
        path: 'local/schedule-center',
        name: 'local-schedule-center',
        component: () => import('../components/schedule/ScheduleCenter.vue'),
        props: { env_id: 'LOCAL' }
      },
      {
        path: 'operations',
        name: 'operations',
        component: () => import('../components/users/UserList.vue')
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem('isAuthenticated')
  if (to.name !== 'login' && !isAuthenticated) next({ name: 'login' })
  else next()
})

export default router
